<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>释放给定的变量</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.unset" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">unset</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">unset</span> &mdash; <span class="dc-title">释放给定的变量</span></p>

   </div>
   
 <div class="refsect1 description" id="refsect1-function.unset-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>unset</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$var</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
  ] ) : <span class="type"><span class="type void">void</span></span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>unset()</strong></span> 销毁指定的变量。
    </p>
    <p class="para">
     <span class="function"><strong>unset()</strong></span> 在函数中的行为会依赖于想要销毁的变量的类型而有所不同。
    </p>
    <p class="para">
     如果在函数中 <span class="function"><strong>unset()</strong></span> 
     一个全局变量，则只是局部变量被销毁，而在调用环境中的变量将保持调用
     <span class="function"><strong>unset()</strong></span> 之前一样的值。
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">destroy_foo</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">destroy_foo</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
      </div>

     </div>
       </p>
  <p class="para">以上例程会输出：</p>
    <p class="para">
     <div class="informalexample">
      <div class="example-contents screen">
<div class="cdata"><pre>
bar
</pre></div>
      </div>
     </div>
    </p>
  <p class="para">
       如果您想在函数中 <span class="function"><strong>unset()</strong></span>
     一个全局变量，可使用 <var class="varname"><var class="varname"><a href="reserved.variables.globals.html" class="classname">$GLOBALS</a></var></var>
     数组来实现：
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">]);<br />}<br /><br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"something"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
    <p class="para">
     如果在函数中 <span class="function"><strong>unset()</strong></span> 
     一个通过引用传递的变量，则只是局部变量被销毁，而在调用环境中的变量将保持调用
     <span class="function"><strong>unset()</strong></span> 之前一样的值。
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"blah"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'something'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
      </div>

     </div>
  </p>
   <p class="para">以上例程会输出：</p>
  <p class="para">
     <div class="informalexample">
      <div class="example-contents screen">
<div class="cdata"><pre>
something
something
</pre></div>
      </div>
     </div>
    </p>
    <p class="para">
     如果在函数中 <span class="function"><strong>unset()</strong></span> 
     一个静态变量，那么在函数内部此静态变量将被销毁。但是，当再次调用此函数时，此静态变量将被复原为上次被销毁之前的值。
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">++;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Before&nbsp;unset:&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">,&nbsp;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">23</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"after&nbsp;unset:&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
      </div>

     </div>
 </p>
   <p class="para">以上例程会输出：</p>
  <p class="para">
     <div class="informalexample">
      <div class="example-contents screen">
<div class="cdata"><pre>
Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
</pre></div>
      </div>
     </div>
    </p>
   </div>

  
 <div class="refsect1 parameters" id="refsect1-function.unset-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">var</code></dt>

     <dd>

      <p class="para">
       要销毁的变量。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">...</code></dt>

     <dd>

      <p class="para">
       其他变量……
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.unset-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.unset-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6373">
    <p><strong>Example #1 <span class="function"><strong>unset()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;销毁单个变量<br /></span><span style="color: #007700">unset&nbsp;(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;销毁单个数组元素<br /></span><span style="color: #007700">unset&nbsp;(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">[</span><span style="color: #DD0000">'quux'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;销毁一个以上的变量<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$foo1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$foo2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$foo3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
   <div class="example" id="example-6374">
    <p><strong>Example #2 使用 <em>(unset)</em> 类型强制转换</strong></p>
    <div class="example-contents"><p>
     <em>(unset)</em> 类型强制转换常常和函数     <span class="function"><strong>unset()</strong></span> 引起困惑。
     为了完整性，<em>(unset)</em> 是作为一个  <em>NULL</em> 类型的强制转换。它不会改变变量的类型。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Felipe'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((unset)&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
NULL
string(6) &quot;Felipe&quot;
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.unset-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">因为是一个语言构造器而不是一个函数，不能被
<a href="functions.variable_functions.html" class="link">可变函数</a> 调用。
</span>
</p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    It is possible to unset even object properties visible in current context.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    在 PHP 5 之前无法在对象里销毁 <em>$this</em>。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    在 <span class="function"><strong>unset()</strong></span> 一个无法访问的对象属性时，如果定义了 <a href="language.oop5.overloading.html#object.unset" class="link">__unset()</a> 则对调用这个重载方法。
   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.unset-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="isset.html" class="function" rel="rdfs-seeAlso">isset()</a> - 检测变量是否已设置并且非 NULL</span></li>
    <li class="member"><span class="function"><a href="empty.html" class="function" rel="rdfs-seeAlso">empty()</a> - 检查一个变量是否为空</span></li>
    <li class="member"><a href="language.oop5.overloading.html#object.unset" class="link">__unset()</a></li>
    <li class="member"><span class="function"><a href="array_splice.html" class="function" rel="rdfs-seeAlso">array_splice()</a> - 去掉数组中的某一部分并用其它值取代</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>